超详细整合SSM框架--(Spring + Spring MVC + MyBatis)
SpringMVC框架--文章跳转
Spring框架--文章跳转
Mybatis框架--文章跳转
整合思路
设计数据库
搭建环境,选择maven工程,选择骨架webapp
导入依赖
创建目录结构,创建domain,controller,service,dao
domain
Controller层
service层
dao层
index页面
Success页面
编写Spring框架
applicationContext文件
Spring整合SpringMVC框架
编写SpringMVC框架
web.xml
Springmvc.xml
整合SpringMVC框架
在web.xml中配置ContextLoaderListener监听器。加载applicationContext.xml文件
Spring整合Mybatis框架
编写Mybatis框架
在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件(AccountDAO接口的方法上添加注解,编写Sql语句)
整合Mybatis框架
把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中,同时配置Spring的声明式事务管理
超详细整合SSM框架--(Spring + Spring MVC + MyBatis)
阅读该文章之前首先要清楚Spring框架,SpringMVC框架,Mybatis框架。
SSM框架,是Spring + Spring MVC + MyBatis的缩写,这个是继SSH之后,目前比较主流的Java EE企业级框架,适用于搭建各种大型的企业级应用系统。
SpringMVC框架--文章跳转
SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring FrameWork 的后续产品,已经融合在 Spring Web Flow 里面。Spring 框架提供了构建 Web
应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用 Spring 进行 WEB 开发时,可以选择使用 Spring 的 Spring MVC 框架或集成其他 MVC 开发框架。
SpringMVC 已经成为目前最主流的 MVC 框架之一,并且随着 Spring3.0 的发布,已成为最优秀的 MVC 框架。
Spring框架--文章跳转
Spring是一个轻量级Java开发框架,最早有Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题。它是一个分层的JavaSE/JavaEE full-stack(一站式)轻量级开源框架,为开发Java应用程序提供全面的基础架构支持。Spring负责基础架构,因此Java开发者可以专注于应用程序的开发。简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
Mybatis框架--文章跳转
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
![](https://img-blog.csdnimg.cn/20210513180523181.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Byb21zaW5n,size_16,color_FFFFFF,t_70)
整合思路
1.先搭建整合的环境
2.把Spring的配置搭建完成
3.再使用Spring整合SpringMVC框架
4.最后使用Spring整合Mybatis框架
设计数据库
CREATE DATABASE ssm;
USE ssm;
CREATE TABLE account ( id INT PRIMARY KEY auto_increment, NAME VARCHAR ( 20 ), money DOUBLE );
搭建环境,选择maven工程,选择骨架webapp
![](https://img-blog.csdnimg.cn/20210505191706117.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Byb21zaW5n,size_16,color_FFFFFF,t_70)
导入依赖
SSM Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
5.0.2.RELEASE
1.6.6
1.2.12
5.1.6
3.4.5
junit
junit
4.11
test
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aop
5.0.2.RELEASE
org.springframework
spring-context
5.0.2.RELEASE
org.springframework
spring-web
5.0.2.RELEASE
org.springframework
spring-webmvc
5.0.2.RELEASE
org.springframework
spring-test
5.0.2.RELEASE
org.springframework
spring-tx
5.0.2.RELEASE
org.springframework
spring-jdbc
5.0.2.RELEASE
junit
junit
4.12
compile
mysql
mysql-connector-java
${mysql.version}
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.0
provided
jstl
jstl
1.2
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.0
c3p0
c3p0
0.9.1.2
jar
compile
创建目录结构,创建domain,controller,service,dao
web依赖于service,service依赖于dao,dao依赖于domain
![](https://img-blog.csdnimg.cn/20210506173610884.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Byb21zaW5n,size_16,color_FFFFFF,t_70)
domain
package com.dynamic.domain;
import java.io.Serializable;
/**
* @Author: Promsing
* @Date: 2021/5/5 - 17:44
* @Description: 实体类-Account
* @version: 1.0
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
Controller层
package com.dynamic.controller;
/**
* @Author: Promsing
* @Date: 2021/5/5 - 17:50
* @Description: Web层账户
* @version: 1.0
*/
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService service;
//需要加 /
@GetMapping("/findAll")
public String findAll(Model model){
System.out.println("表现层查询所有信息!");
//调用Service方法
List all = service.findAll();
for (Account account : all) {
System.out.println(account);
}
model.addAttribute("all",all);
return "success";
}
@PostMapping("/save")
public String save(Account account){
service.saveAccount(account);
return "success";
}
}
service层
public interface AccountService {
/**
* 查询所有
* @return
*/
public List findAll();
/**
* 保存账户信息
* @param account
*/
public void saveAccount(Account account);
}
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao dao;
@Override
public List findAll() {
System.out.println("业务层:查询所有信息!");
return dao.findAll();
}
@Override
public void saveAccount(Account account) {
System.out.println("业务层:保存账户。。。");
dao.saveAccount(account);
}
}
dao层
/**
* @Author: Promsing
* @Date: 2021/5/5 - 17:46
* @Description: DAO层 使用注解
* @version: 1.0
*/
@Repository
public interface AccountDao {
/**
* 查询所有
* @return
*/
@Select("select * from account")
public List findAll();
/**
* 保存账户信息
* @param account
*/
@Insert("insert into account (name,money) values(#{name},#{money})")
public void saveAccount(Account account);
}
index页面
首页
测试查询
姓名:
金额:
Success页面
成功页面
成功页面
${all}
${account.name}
${account.money}
编写Spring框架
applicationContext文件
Spring整合SpringMVC框架
编写SpringMVC框架
web.xml
Archetype Created Web Application
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
Springmvc.xml
整合SpringMVC框架
在Controller中能够成功调用service对象中的方法
![](https://img-blog.csdnimg.cn/2021050520044815.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Byb21zaW5n,size_16,color_FFFFFF,t_70)
在web.xml中配置ContextLoaderListener监听器。加载applicationContext.xml文件
在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器。(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)
Archetype Created Web Application
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
Spring整合Mybatis框架
编写Mybatis框架
在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件(AccountDAO接口的方法上添加注解,编写Sql语句)
整合Mybatis框架
把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中,同时配置Spring的声明式事务管理
如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。原创不易,转载请联系作者!
|